﻿using SqlSugar;

namespace VueEleAdminFurion
{
    public static class SqlsugarSetup
    {

        public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration, string dbName = "SqlServerConnectionString")
        {
            //如果多个数数据库传 List<ConnectionConfig>
            var configConnection = new ConnectionConfig()
            {
                ConfigId = dbName,
                DbType = SqlSugar.DbType.SqlServer,
                ConnectionString = configuration.GetConnectionString(dbName),
                IsAutoCloseConnection = true,
            };

            var wcc = new ConnectionConfig()
            {
                ConfigId="Wcc",
                DbType = SqlSugar.DbType.SqlServer,
                ConnectionString = configuration.GetConnectionString("WCC"),
                IsAutoCloseConnection = true,
            };


            var connectionConfigs = new List<ConnectionConfig>() {configConnection,wcc };


            SqlSugarScope sqlSugar = new SqlSugarScope(connectionConfigs,
                db =>
                {
                //单例参数配置，所有上下文生效
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                    //Console.WriteLine(sql);//输出sql
                    };
                });

            services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
        }
    }
}
